﻿
@{
    ViewData["Title"] = "权限维护";
}
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layuiadmin-btn-list" data-type="btnrefesh">刷选</button>
                <button class="layui-btn layuiadmin-btn-list" data-type="add">添加</button>
            </div>
            <ul id="rightTree" class="ztree"></ul>
        </div>
    </div>
</div>
<style type="text/css">
    .ztree li span.button.add {
        margin-left: 2px;
        margin-right: -1px;
        background-position: -144px 0;
        vertical-align: top;
        *vertical-align: middle
    }
</style>
<link href="@Url.Content("~/lib/zTree/css/demo.css")" rel="stylesheet" />
<link href="@Url.Content("~/lib/zTree/css/bootstrapStyle/bootstrapStyle.css")" rel="stylesheet" />
<script src="@Url.Content("~/lib/zTree/js/jquery.ztree.all.min.js")"></script>
<script type="text/javascript">
    var layer, admin, $;
    layui.config({
        base: '/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'layer'], function () {
        $ = layui.$;
        admin = layui.admin;
        layer = layui.layer;
        InitPms();

        var $ = layui.$, active = {
            btnrefesh: function () {
                InitPms();
            },
            add: function () {
                layer.open({
                    type: 2
                    , title: '添加权限'
                    , content: '/Pms/PmsAdd'
                    , area: ["800px", "600px"]
                    , maxmin: true
                });
            }
        };

        $('.layui-btn.layuiadmin-btn-list').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });

    function InitPms() {
        admin.req({
            url: '/Pms/GetsAll' //实际使用请改成服务端真实接口
            , type: 'post'
            , done: function (res) {
                initzTree(res.data);
            }
        });
    }

    function initzTree(data) {
        var setting = {
            data: {
                key: {
                    name: "PmsName",
                    title: "PmsName",
                    checked: "Checked"
                },
                simpleData: {
                    enable: true,
                    idKey: "Id",
                    pIdKey: "ParentId",
                    rootPId: 0
                }
            },
            view: {
                addHoverDom: addHoverDom,
                removeHoverDom: removeHoverDom,
                selectedMulti: false
            },
            edit: {
                enable: true
            }, callback: {
                beforeEditName: beforeEditName,
                beforeRemove: beforeRemove
            }
        };
        $.fn.zTree.init($("#rightTree"), setting, data);
    }
    function beforeRemove(treeId, treeNode) {
        layer.confirm('真的删除行么', function (index) {
            admin.req({
                url: "/Pms/DelByIds1?Ids=" + treeNode.Id
                , done: function (res) {
                    layer.alert(res.msg);
                    InitPms();
                }
            });
        });
        return false;
    }
    function beforeEditName(treeId, treeNode) {
        layer.open({
            type: 2
            , title: '编辑权限'
            , content: '/Pms/PmsAdd?Id=' + treeNode.Id
            , area: ["800px", "600px"]
            , maxmin: true
        });
        return false;
    }

    function addHoverDom(treeId, treeNode) {
        var sObj = $("#" + treeNode.tId + "_span");
        if (treeNode.editNameFlag || $("#addBtn_" + treeNode.tId).length > 0) return;
        var addStr = "<span class='button add' id='addBtn_" + treeNode.tId
            + "' title='add node' onfocus='this.blur();'></span>";
        sObj.after(addStr);
        var btn = $("#addBtn_" + treeNode.tId);
        if (btn) btn.bind("click", function () {
            layer.open({
                type: 2
                , title: '添加权限'
                , content: '/Pms/PmsAdd?ParentId=' + treeNode.Id
                , area: ["800px", "600px"]
                , maxmin: true
            });
            return false;
        });
    };
    function removeHoverDom(treeId, treeNode) {
        $("#addBtn_" + treeNode.tId).unbind().remove();
    };
</script>
